package web.protocols.action;

import com.opensymphony.xwork2.ActionSupport;
import domain.entities.Budget;
import domain.entities.Compromisopais;
import domain.entities.Protocolo;
import domain.entities.ProtocoloCompromisopais;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import services.BudgetService;
import services.ProtocolCRUDService;
import services.ProtocoloCompromisopaisService;
import services.exceptions.NoProtocoloCompromisopaisSelectedException;


import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: vgonzalez
 * Date: 14/09/12
 * Time: 16:29
 * To change this template use File | Settings | File Templates.
 */

@Namespace("/Protocols/ProtocolBudget")
public class ProtocolBudgetController extends ActionSupport {
    private ProtocolCRUDService protocolService;
    //private ProtocoloCompromisopais protocoloCompr;
    private Protocolo protocoloRecovered;
    private ProtocoloCompromisopaisService protocoloCompromisopaisService;
    private BudgetService budgetService;
    private List<Budget> budgets;
    private List<Compromisopais> paisList;
    private Integer budId;
    private Budget budget = new Budget();
    private String countryId;
    private String sbwInitial;
    private String sbwFinal;
    private String sbwRevised;

    @Autowired
    public ProtocolBudgetController(ProtocolCRUDService protocolService, BudgetService budgetService, ProtocoloCompromisopaisService protocoloCompromisopaisService){
        this.protocolService = protocolService;
        this.budgetService = budgetService;
        this.protocoloCompromisopaisService = protocoloCompromisopaisService;
    }

    private String cargarTabla(){
        try{
            //protocoloCompr = protocolService.recoverProtocoloCompromisopais();
            protocoloRecovered = protocolService.recoverProtocolo();
        }
        catch (NoProtocoloCompromisopaisSelectedException e){
            e.printStackTrace();
        }
        if(protocoloRecovered == null)
            return null;

        this.paisList = protocoloCompromisopaisService.findCompromisoPaisByProtocoloId(protocoloRecovered.getId());
        budgets = budgetService.findByProtocolo(protocoloRecovered);

        return SUCCESS;
    }

    @Action(value="clean", results={
            @Result(name="success", location="main", type="redirectAction")
    })
    public String clean() {
        countryId = null;
        return SUCCESS;
    }

        @Action(value="main", results={
            @Result(name="success", location="/protocols.protocolbudget.tiles", type="tiles")
    })
    public String execute() {
       if(cargarTabla() == null)
           return null;

        if(countryId == null || countryId.equals("")){
            budget = null;
            sbwFinal = null;
            sbwRevised = null;
            sbwInitial = null;
        }
        else{
            budget = budgetService.findByCompromisopais(Integer.parseInt(countryId));
            if(budget == null){
                budget = new Budget();
                ProtocoloCompromisopais pcp = protocoloCompromisopaisService.getByProtocoloIdCompromisopaisId(protocoloRecovered.getId(), Integer.parseInt(countryId));
                budget.setProtocoloCompromisopais(pcp);
            }
            else{
                if(budget.getAmountSbwfinal() != null)
                    sbwFinal = budget.getAmountSbwfinal().toString();
                else
                    sbwFinal = null;
                if(budget.getAmountSbwrevised() != null)
                    sbwRevised = budget.getAmountSbwrevised().toString();
                else
                    sbwRevised = null;
                if(budget.getAmountSbwinitial() != null)
                    sbwInitial = budget.getAmountSbwinitial().toString();
                else
                    sbwInitial = null;
            }
        }
        return SUCCESS;
    }


    @Action(value="save", results={
            @Result(name="success", location="main", type="redirectAction")
    })
    public String save() {
        if(cargarTabla() == null)
            return null;

        ProtocoloCompromisopais pcp = protocoloCompromisopaisService.getByProtocoloIdCompromisopaisId(protocoloRecovered.getId(), Integer.parseInt(countryId));
        budget.setProtocoloCompromisopais(pcp);

        budget.setAmountSbwinitial(Float.parseFloat(sbwInitial));
        if(!sbwFinal.equals(""))
            budget.setAmountSbwfinal(Float.parseFloat(sbwFinal));
        else
            budget.setAmountSbwfinal(null);
        if(!sbwRevised.equals(""))
            budget.setAmountSbwrevised(Float.parseFloat(sbwRevised));
        else
            budget.setAmountSbwrevised(null);


        if(budget.getId() == null || budget.getId() <= 0){
            budgetService.save(budget);
        }
        else
            budgetService.attachDirty(budget);
        countryId = null;

        return SUCCESS;
    }

    public String getSbwRevised() {
        return sbwRevised;
    }

    public void setSbwRevised(String sbwRevised) {
        this.sbwRevised = sbwRevised;
    }

    public String getSbwInitial() {
        return sbwInitial;
    }

    public void setSbwInitial(String sbwInitial) {
        this.sbwInitial = sbwInitial;
    }

    public String getSbwFinal() {
        return sbwFinal;
    }

    public void setSbwFinal(String sbwFinal) {
        this.sbwFinal = sbwFinal;
    }

    public List<Budget> getBudgets() {
        return budgets;
    }

    public void setBudgets(List<Budget> budgets) {
        this.budgets = budgets;
    }

    public List<Compromisopais> getPaisList() {
        return paisList;
    }

    public void setPaisList(List<Compromisopais> paisList) {
        this.paisList = paisList;
    }

    public Budget getBudget() {
        return budget;
    }

    public void setBudget(Budget budget) {
        this.budget = budget;
    }

    public Integer getBudId() {
        return budId;
    }

    public void setBudId(Integer budId) {
        this.budId = budId;
    }

    public String getCountryId() {
        return countryId;
    }

    public void setCountryId(String countryId) {
        this.countryId = countryId;
    }

}
